package com.cats.template.sys.exception;

import com.cats.template.sys.enums.ResponseEnum;
import com.cats.template.sys.utils.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @author duxiaobo
 * @date 2021/8/92:54 下午
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {Exception.class, CatException.class})
    public ResponseVO exceptionHandler(Exception e) {
        log.error("异常信息：{}", e.getMessage());
        e.printStackTrace();
        return new ResponseVO(ResponseEnum.ERROR.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = {AuthorizationException.class,
            LockedAccountException.class,
            UnknownAccountException.class,
            AuthenticationException.class
    })
    public ResponseVO unauthenticatedExceptionHandler(Exception e) {
        log.error("异常信息：{}", e.getMessage());
        e.printStackTrace();
        return new ResponseVO(ResponseEnum.AUTHENTICATION_FAILURE.getCode(), ResponseEnum.AUTHENTICATION_FAILURE.getMsg());
    }


}
